﻿// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information.
// Website: https://admin.blazor.zone

using BlazorAdmin.Models;
using BlazorAdmin.Interface;
using BootstrapBlazor.Components;
using BlazorAdmin.Web.Shared.Extensions;
using BlazorAdmin.Web.Shared.Services;
using Microsoft.AspNetCore.Components;
using System.Diagnostics.CodeAnalysis;

namespace BlazorAdmin.Web.Shared.Pages.Admin
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Roles
    {
        [Inject]
        [NotNull]
        private DialogService? DialogService { get; set; }

        [Inject]
        [NotNull]
        private ToastService? ToastService { get; set; }

        [Inject]
        [NotNull]
        private IGroup? GroupService { get; set; }

        [Inject]
        [NotNull]
        private IUser? UserService { get; set; }

        [Inject]
        [NotNull]
        private IApp? AppService { get; set; }

        [Inject]
        [NotNull]
        private IDict? DictService { get; set; }

        [Inject]
        [NotNull]
        private INavigation? NavigationService { get; set; }

        [Inject]
        [NotNull]
        private BootstrapAppContext? AppContext { get; set; }

        private async Task OnAssignmentUsers(Role role)
        {
            List<SelectedItem> userItems = UserService.GetAll().ToSelectedItemList();
            List<int> roles = UserService.GetUsersByRoleId(role.ID);

            await DialogService.ShowAssignmentDialog($"分配用户 - 当前角色: {role.RoleName}", userItems, roles, () =>
            {
                IEnumerable<int> ids = from u in userItems where u.Active select int.Parse(u.Value);
                bool result = UserService.SaveUsersByRoleId(role.ID, ids);
                return Task.FromResult(result);
            }, ToastService);
        }

        private async Task OnAssignmentGroups(Role role)
        {
            List<SelectedItem> groupItems = GroupService.GetAll().ToSelectedItemList();
            List<int> roles = GroupService.GetGroupsByRoleId(role.ID);

            await DialogService.ShowAssignmentDialog($"分配部门 - 当前角色: {role.RoleName}", groupItems, roles, () =>
            {
                IEnumerable<int> ids = from u in groupItems where u.Active select int.Parse(u.Value);
                bool result = GroupService.SaveGroupsByRoleId(role.ID, ids);
                return Task.FromResult(result);
            }, ToastService);
        }

        private async Task OnAssignmentMenus(Role role)
        {
            List<Navigation> menus = NavigationService.GetAllMenus(AppContext.LoginName);
            List<int> values = NavigationService.GetMenusByRoleId(role.ID);

            await DialogService.ShowNavigationDialog($"分配菜单 - 当前角色: {role.RoleName}", menus, values, items =>
            {
                bool result = NavigationService.SaveMenusByRoleId(role.ID, items);
                return Task.FromResult(result);
            }, ToastService);
        }

        private async Task OnAssignmentApps(Role role)
        {
            List<SelectedItem> apps = DictService.GetApps().ToSelectedItemList();
            List<string> values = AppService.GetAppsByRoleId(role.ID);

            await DialogService.ShowAssignmentDialog($"分配应用 - 当前角色: {role.RoleName}", apps, values, () =>
            {
                IEnumerable<string> ids = from u in apps where u.Active select u.Value;
                bool result = AppService.SaveAppsByRoleId(role.ID, ids);
                return Task.FromResult(result);
            }, ToastService);
        }
    }
}